﻿using System.Windows ;
using System.Windows.Interactivity ;
using System.Linq ;
using OpenLightGroup.Common.LinqToVisualTree ;
using System.Diagnostics ;

namespace OpenLightGroup.Common.Behaviors
    {
        public class LinqAttachDataContextBehavior : Behavior<FrameworkElement>
            {
                public LinqAttachDataContextBehavior ( )
                    {

                    }

                #region Properties

                public string ElementName
                {
                    get
                        {
                            return (string)this.GetValue( ElementNameProperty ) ;
                        }
                    set
                        {
                            this.SetValue( ElementNameProperty, value ) ;
                        }
                }

                // Using a DependencyProperty as the backing store for ElementName.  This enables animation, styling, binding, etc...
                public static readonly DependencyProperty ElementNameProperty =
                DependencyProperty.Register( "ElementName", typeof(string), typeof(AttachDataContextBehavior), new PropertyMetadata( null ) ) ;

                public object DataContextType
                {
                    get
                        {
                            return this.GetValue( DataContextTypeProperty ) ;
                        }
                    set
                        {
                            this.SetValue( DataContextTypeProperty, value ) ;
                        }
                }

                // Using a DependencyProperty as the backing store for DataContextType.  This enables animation, styling, binding, etc...
                public static readonly DependencyProperty DataContextTypeProperty =
                DependencyProperty.Register( "DataContextType", typeof(object), typeof(AttachDataContextBehavior), new PropertyMetadata( null ) ) ;

                #endregion

                protected override void OnAttached ( )
                    {
                        base.OnAttached( ) ;

                        FrameworkElement root = Application.Current.RootVisual as FrameworkElement ;

                        var matches = from FrameworkElement e in root.Descendants<FrameworkElement>( )
                                      where e.Name == this.ElementName && e.DataContext.GetType( ) == this.DataContextType.GetType( )
                                      select e ;

                        if ( matches != null && matches.Count( ) == 1 )
                        {

                            this.AssociatedObject.DataContext = matches.First( ).DataContext ;

                        }
                        else if ( matches != null && matches.Count( ) > 1 )
                        {

                            this.AssociatedObject.DataContext = matches.First( ).DataContext ;

                            //Found too many... using first one....                
                            Debug.WriteLine( "Found too many matches, using the first one...." ) ;

                        }
                        else
                        {

                            //Nothing found 
                            Debug.WriteLine( "Could not locate a match." ) ;

                        }

                    }

            }
    }